package com.sml.aop.sml;

import com.sml.requset.ApiRequest;
import com.sml.util.DateUtil;
import java.util.Date;
import java.util.UUID;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StringUtils;

/**
 * @Author linlx
 * @Date 2021/8/13 8:42
 * @Description feign接口切面，实现唯一标识的生成
 **/
@Aspect
public class SMLFeignAspect {

  private String serverName;

  public SMLFeignAspect(String serverName) {
    this.serverName = serverName;
  }

  // 切点为项目feignClient包的位置
  @Pointcut("@annotation(SMLFeignAnnotation)")
  private void pointcut() {

  }

  @Before(value = "pointcut() && args(apiRequest)")
  public void beforeAdvice(ApiRequest<?> apiRequest) {
    // 给请求生成唯一标识
    if (StringUtils.isEmpty(apiRequest.getRequestId())) {
      apiRequest.setRequestId(
          "origin-serverName:" + serverName + ":"
              + DateUtil.getRequestDateFormat(new Date())
              + ":"
              + UUID.randomUUID().toString().substring(0, 10));
    }
  }
}
